package com.jinkaimen.kqb.ui.weiget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.View;
import android.widget.GridView;

import com.jinkaimen.kqb.R;

/**
 * Created by lixuebo on 17/7/8.
 */

public class DiverGridView extends GridView {
    /**
     * 默认的分割线的颜色
     * 也可以在布局中设置
     */
    private int diverColor = R.color.bg_operater_selected;
    /**
     * 默认的分割线的高度
     * 也可以在布局中设置
     */
    private int diverHeight = 1;
    /**
     * 所使用的画笔
     */
    private Paint paint;

    private Context context;

    public DiverGridView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
//        TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.gv_acusttrs);
//        diverHeight = (int) typedArray.getDimension(R.styleable.gv_acusttrs_divierHeight,10);
//        diverColor = typedArray.getResourceId(R.styleable.gv_acusttrs_divierColor,R.drawable.editext_slelect_black4);
//        typedArray.recycle();
        paint = new Paint();
        paint.setColor(ContextCompat.getColor(context, diverColor));
        paint.setStyle(Paint.Style.STROKE);
        paint.setAntiAlias(true);
        paint.setStrokeWidth(diverHeight);
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
    }

    /**
     * 动态修改默认的分割线的颜色
     */
    public void setDiverColor(int diverColor) {
        this.diverColor = diverColor;
        invalidate();
    }

    /**
     * 动态修改默认的分割线的颜色
     */
    public void setDiverHeight(int diverHeight) {
        this.diverHeight = diverHeight;
        invalidate();
    }

    /**
     * @param canvas
     */

  /*  @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Rect rect = new Rect();
        rect.left = DensityUtil.getScreenIntWidth(context) / 4;
        rect.top = DensityUtil.getScreenIntHeight(context) / 4;
        rect.right = DensityUtil.getScreenIntWidth(context)/ 4 * 3;
        rect.bottom = DensityUtil.getScreenIntHeight(context)/ 4 * 3;
        canvas.drawRect(rect,paint);
    }*/
    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
        View localView1 = getChildAt(0);//得到第一个view
        int column = getWidth() / localView1.getWidth();//列数
        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            View cellView = getChildAt(i);
            if ((i + 1) % column == 0) {//每一行最后一个
                canvas.drawLine(cellView.getLeft(), cellView.getBottom(), cellView.getRight(), cellView.getBottom(), paint);
            } else if ((i + 1) > (childCount - (childCount % column))) {//最后一行的item
                canvas.drawLine(cellView.getRight(), cellView.getTop(), cellView.getRight(), cellView.getBottom(), paint);
            } else {
                canvas.drawLine(cellView.getRight(), cellView.getTop(), cellView.getRight(), cellView.getBottom(), paint);
                canvas.drawLine(cellView.getLeft(), cellView.getBottom(), cellView.getRight(), cellView.getBottom(), paint);
            }
        }
    }
}
